from django.shortcuts import render,get_object_or_404
from django.http import HttpResponseRedirect,Http404
from django.urls import reverse
from django.contrib.auth import logout,login,authenticate
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

from .models import UserPage,CommentUser,Avatar
from .forms import UserPageForm,CommentUserForm,AvatarForm

import markdown

@login_required	
def comment(request,user_id):
	userobj = get_object_or_404(User,id=user_id)
	if request.method != 'POST':
		raise Http404
	else:
		form = CommentUserForm(request.POST)
		if form.is_valid():
			new_comment = form.save(commit=False)
			new_comment.name = userobj.username
			new_comment.Owner = request.user
			new_comment.save()
			return HttpResponseRedirect(reverse('users:homePage',
									args=[user_id]))
	
@login_required	
def delete_comment(request,comment_id):
	comment = get_object_or_404(CommentUser,id=comment_id)
	commenter = comment.Owner
	homer = User.objects.get(username=comment.name)
	print(commenter)
	print(homer)
	print(request.user)
	if not (homer == request.user or commenter == request.user or request.user.is_superuser):
		raise Http404
	comment.delete()
	return HttpResponseRedirect(reverse('users:homePage',
								args=[homer.id]))

										
def logout_view(request):
	logout(request)
	return HttpResponseRedirect(reverse('blogs:index'))

def register(request):
	if request.method != 'POST':
		form = UserCreationForm()
	else:
		form = UserCreationForm(data=request.POST)
		
		if form.is_valid():
			new_user = form.save()
			user = authenticate(username=new_user.username,
				password=request.POST['password1'])
			login(request,user)
			return HttpResponseRedirect(reverse('blogs:index'))
	context = {'form':form}
	return render(request,'users/register.html',context)
	
def homePage(request,user_id):
	userobj = get_object_or_404(User,id=user_id)
	try:
		home = UserPage.objects.get(id=user_id)
		form = UserPageForm(instance=home)
		homeMD = markdown.markdown(
			home.text,
			extensions=[
				'markdown.extensions.extra',
				'markdown.extensions.codehilite',
				'markdown.extensions.toc',
			]
		)
	except:
		home = ''
		form = UserPageForm()
		homeMD = ''
	comments = CommentUser.objects.filter(name=userobj.username)
	try:
		items = Avatar.objects.filter(name=userobj.username)
		header = items[len(items)-1].image.url
	except:
		header = '/medias/img/default.jpg'
	
	commentForm = CommentUserForm()
		
	context = {'home':home,'comments':comments,'form':form,'userobj':userobj,'header':header,'commentForm':commentForm,'homeMD':homeMD}
	return render(request,'users/home.html',context)

@login_required		
def edit_home(request):
	try:
		home = UserPage.objects.get(id=request.user.id)
		err = 0
	except:
		home = ''
		err = 1
	if request.method != 'POST':
		raise Http404
	else:
		if err:
			form = UserPageForm(request.POST)
		else:
			form = UserPageForm(instance=home,data=request.POST)
		if form.is_valid():
			page = form.save(commit=False)
			page.Owner = request.user
			page.save()
			return HttpResponseRedirect(reverse('users:homePage',
										args=[request.user.id]))

@login_required											
def upload_avatar(request):
	if request.method != 'POST':
		form = AvatarForm()
	else:
		name = request.user.username
		image = request.FILES.get('image')
		Avatar.objects.create(name=name,image=image)
		return HttpResponseRedirect(reverse('users:homePage',
								args=[request.user.id]))
	context = {'form':form}
	return render(request,'users/uploadAvatar.html',context)
	
	

